package com.zleetcode.队列;

import java.util.Stack;

/**
 * https://leetcode-cn.com/problems/implement-queue-using-stacks/
 */
public class _232_用栈实现队列 {
    Stack<Integer> pushStack;
    Stack<Integer> popStack;

    public _232_用栈实现队列() {
        pushStack = new Stack<>();
        popStack = new Stack<>();
    }

    /**
     * 入队
     */
    public void push(int x) {
        // 全部放入push栈中
        pushStack.push(x);
    }

    /**
     * 出队
     */
    public int pop() {
        checkEmpty();
        return popStack.pop();
    }

    /**
     * 查看队列头部元素
     */
    public int peek() {
        checkEmpty();
        return popStack.peek();
    }

    /**
     * 队列是否为空
     */
    public boolean empty() {
        return popStack.isEmpty() && pushStack.empty();
    }

    /**
     * 检查pop栈是否为空，如果为空就把push栈中的元素全部倒入到pop栈中
     */
    private void checkEmpty() {
        if (popStack.isEmpty()) {
            while (!pushStack.isEmpty()) {
                // 全部倒入pop栈中
                popStack.push(pushStack.pop());
            }
        }
    }

    public static void main(String[] args) {
        _232_用栈实现队列 queue = new _232_用栈实现队列();
        queue.push(11);
        queue.push(22);
        queue.push(33);
        queue.push(44);
        System.out.println(queue.peek());
        while (!queue.empty()) {
            System.out.println(queue.pop());
        }
    }
}
